package com.mirror.llk.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.mirror.llk.R;
import com.mirror.llk.utils.Constant;
import com.mirror.utils.adapter.CustomComplexAdapter;
import com.mirror.utils.adapter.CustomPagerAdapter;
import com.mirror.utils.ui.UiUtils;
import com.mirror.utils.view.CustomListView;
import com.mirror.utils.view.CustomViewPager;

public class ThemeDownloadView extends LinearLayout {

	private static final String TAG = "ThemeDownloadView";

	private Context context;

	private float drift = 0;

	private int index = 0;

	public RelativeLayout rlOfficial;

	public RelativeLayout rlPrivate;

	public View llOfficialLoading;

	public View llPrivateLoading;

	public CustomListView lvOfficial;

	public CustomListView lvPrivate;

	public CustomComplexAdapter officalAdapter;

	public CustomComplexAdapter privateAdapter;

	public ImageView ivSlider;

	public CustomViewPager vpArea;

	private CustomPagerAdapter pagerAdapter;

	public ThemeDownloadView(Context context) {
		this(context, null);
	}

	@SuppressWarnings("deprecation")
	public ThemeDownloadView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.theme_download, this, true);
		setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
		this.init();
	}

	private void init() {
		drift = UiUtils.getDisplaySize(context)[0] / 2;

		rlOfficial = (RelativeLayout) this.findViewById(R.id.rl_themeDownload_offical);
		rlPrivate = (RelativeLayout) this.findViewById(R.id.rl_themeDownload_private);

		ivSlider = (ImageView) this.findViewById(R.id.iv_tab_slider);
		ivSlider.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.tab_slider), (int) drift, 10, true));

		rlOfficial.setOnClickListener(listener);
		rlPrivate.setOnClickListener(listener);

		initViewPager();
	}

	private void initViewPager() {
		vpArea = (CustomViewPager) this.findViewById(R.id.vp_themeDownload_area);
		List<View> viewList = new ArrayList<View>();

		View officialView = LayoutInflater.from(this.getContext()).inflate(R.layout.theme_download_official, null);
		View privateView = LayoutInflater.from(this.getContext()).inflate(R.layout.theme_download_private, null);

		viewList.add(officialView);
		viewList.add(privateView);

		lvOfficial = (CustomListView) officialView.findViewById(R.id.lv_themeDownload_offical);
		lvPrivate = (CustomListView) privateView.findViewById(R.id.lv_themeDownload_private);

		officalAdapter = new CustomComplexAdapter(context, new ArrayList<HashMap<String, Object>>(), R.layout.theme_download_item,
		        new String[] { "themeName" }, new int[] { R.id.tv_downloadItem_themeName });
		privateAdapter = new CustomComplexAdapter(context, new ArrayList<HashMap<String, Object>>(), R.layout.theme_download_item,
		        new String[] { "themeName" }, new int[] { R.id.tv_downloadItem_themeName });

		lvOfficial.setAdapter(officalAdapter);
		lvPrivate.setAdapter(privateAdapter);

		pagerAdapter = new CustomPagerAdapter(viewList);
		vpArea.setAdapter(pagerAdapter);
		vpArea.setOnPageChangeListener(new MyOnPageChangeListener());
		vpArea.setDisallowIntecept(false);
	}

	/**
	 * 监听初始化
	 */
	private View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.rl_themeDownload_offical: {
					vpArea.setCurrentItem(0);
					break;
				}
				case R.id.rl_themeDownload_private: {
					vpArea.setCurrentItem(1);
					break;
				}
				default:
					Log.e(TAG, Constant.UNTREATED_EVENT_EXCEPTION);
					break;
			}
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int select) {
			Animation animation = null;
			switch (select) {
				case 0: {
					if (index == 1) {
						animation = new TranslateAnimation(drift + 1, 0, 0, 0);
					}
					break;
				}
				case 1: {
					if (index == 0) {
						animation = new TranslateAnimation(0, drift + 1, 0, 0);
					}
					break;
				}
			}
			index = select;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivSlider.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

}
